package com.example.Cairodio;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import com.example.entities.Comments;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;


/**
 * 
 * @author Mariam
 * 
 * This is the comments activity, from which you can view all the previous comments on a specific place
 * and add a new comment.
 *
 */

public class CommentsActivity extends Activity {
	
	  static final String    SERVER    = "http://cairodio-app.appspot.com";
	  CairodioDbAdapter dbHelper;
	  private ListView commentsListView;
	  private EditText addComment;
	  private Button postComment;
	  private ImageButton mapButton;
	  private TextView placeNameLabel;
	  private int placeID;
	  private String placeName;
	  private ArrayList<Comments> comments;
	  private CommentsListAdapter adapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
	  super.onCreate(savedInstanceState);
	  setContentView(R.layout.comments_list);
	  
	  dbHelper = ((CairodioApplication) getApplication()).getAdapter();

	  placeNameLabel = (TextView) findViewById(R.id.placeNameLabel);
	  addComment = (EditText) findViewById(R.id.addComment);
	  postComment = (Button) findViewById(R.id.post); 
	  commentsListView = (ListView) findViewById(R.id.commentsList01);
	  
	  Bundle b = getIntent().getExtras();
	  placeID = b.getInt("placeID");
	  placeName = b.getString("placeName");
	  
	  placeNameLabel.setText(placeName);
	  
	  comments = getPlaceComments();
  
	  adapter = new CommentsListAdapter(this, comments);
	  commentsListView.setAdapter(adapter);
	  
//	  dbHelper.deletePlaceComments(placeID);
	  
	  postComment.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				
				String input = addComment.getText().toString();
				if(!input.equals("")){
					String placeName=dbHelper.getPlaceName(placeID);
				dbHelper.insertComment(input, placeID);
				comments = getPlaceComments();
				
				Log.e("LIST", ">>>>>>> size " + comments.size());
				adapter.notifyDataSetChanged();
				startActivity(getIntent()); finish();
				
					try {
						
						HttpClient httpclient = new DefaultHttpClient();
						HttpPost httppost = new HttpPost(SERVER + "/addComment");

						List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
								2);
						nameValuePairs.add(new BasicNameValuePair("place",
								placeName));

						nameValuePairs
								.add(new BasicNameValuePair("comment", input));

						httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

						// Execute HTTP Post Request
						@SuppressWarnings("unused")
						HttpResponse response = httpclient.execute(httppost);

						
					} catch (Exception e) {
						e.printStackTrace();
				
											}
				
				
				
				
				}
			}
		});	
	  
	  mapButton = (ImageButton) findViewById(R.id.commentsMap);
		mapButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {	

				Intent returnIntent = new Intent();
				setResult(RESULT_OK,returnIntent);       
				finish();
			}
		});
	}
	
	/**
	 * @author Mariam
	 * 
	 * This method returns an ArrayList of comments,
	 * and sets the comment text and date in each cell in the list view
	 * @return
	 */
	@SuppressWarnings("static-access")
	private ArrayList<Comments> getPlaceComments() {

	     ArrayList<Comments> results = new ArrayList<Comments>();
	     
			Cursor c = dbHelper.getAllComments(placeID);
			c.moveToFirst();
			while (!c.isAfterLast()) {
				
			     Comments cmnt = new Comments();
			     cmnt.setComment(c.getString(c.getColumnIndex(dbHelper.colComment)));
			     Log.e("LIST", ">>>>>>> gotten " + c.getString(c.getColumnIndex(dbHelper.colComment)));
			     cmnt.setDate(c.getString(c.getColumnIndex(dbHelper.colDate)));
			     results.add(cmnt);
			     
				c.moveToNext();
			}
	    	     
	     return results;
	} 
	
	
	public void onStart() {
		super.onStart();
		stopService(CairodioMapActivity.intent);

	}

	@Override
	public void onPause() {
		super.onPause();
		startService(CairodioMapActivity.intent);
	}

	@Override
	public void onResume() {
		super.onResume();
	}

	@Override
	public void onStop() {
		super.onStop();
		
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		stopService(CairodioMapActivity.intent);	
		}
	
}
